package com.ruoyi.classai.utils;

import com.ruoyi.common.core.exception.GlobalException;
import org.apache.commons.lang3.StringUtils;

public class FileUtil {

    /**
     * 获取资源文件后缀
     */
    public static String getResourceSuffix(String resourceUrl) {
        if (resourceUrl == null || resourceUrl.isEmpty()) {
            return "";
        }

        int lastDotIndex = resourceUrl.lastIndexOf('.');
        if (lastDotIndex == -1 || lastDotIndex == resourceUrl.length() - 1) {
            return "";
        }

        return resourceUrl.substring(lastDotIndex + 1);
    }

    /**
     * 获取文件类型
     */
    public static Integer getFileType(String resourceSuffix) {
        if (StringUtils.isEmpty(resourceSuffix)) {
            throw new GlobalException("未获取到资源文件类型");
        }
        resourceSuffix = resourceSuffix.toUpperCase();
        switch (resourceSuffix) {
            case "PNG":
            case "JPEG":
            case "JPG":
                return 0;
            case "MP4":
            case "WMV":
            case "AVI":
            case "MPEG":
                return 1;
            case "MP3":
                return 2;
            default:
                throw new GlobalException("不支持的文件类型");
        }
    }
}
